package com.xusiyan08.controller; 

import com.xusiyan08.entity.Item;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "cloud-second-hand", url = "${feign.client.item-service.url:http://localhost:8083}", fallback = ItemFeignClientFallback.class)
public interface ItemFeignClient {

    @GetMapping("/api/items/{itemId}")
    @io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker(name = "itemService", fallbackMethod = "getItemByIdFallback")
    ResponseEntity<Item> getItemById(@PathVariable("itemId") Long itemId);

    default ResponseEntity<Item> getItemByIdFallback(Long itemId, Throwable t) {
        // This default method allows the fallback to be defined within the fallback class
        // The actual fallback logic is in ItemFeignClientFallback.java
        // This is a way to connect the annotation to the Feign fallback mechanism.
        // The primary fallback mechanism is the 'fallback' attribute in @FeignClient.
        // This annotation mainly controls the circuit breaker configuration from application.yml.
        return new ItemFeignClientFallback().getItemById(itemId);
    }
}
